<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    Netty WebSocket 时间服务器
</head>
<br>
<body>
<br>
<script type="text/javascript" src="../js/jquery-1.12.0.js"></script>
<script type="text/javascript">

    var socket;
    var userId = "usrId001";
    var password = "1111";
    var token= "";
    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:9000/");
        socket.onmessage = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = "";
            var obj = $.parseJSON( event.data);
            ta.value = obj.msg;
            if( obj.result&&obj.result.token){
            	token = obj.result.token
            	console.log(token);
            }
            
        };
        socket.onopen = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = "打开WebSocket服务正常，浏览器支持WebSocket!";
        };
        socket.onclose = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = "";
            ta.value = "WebSocket 关闭!";
        };
    }
    else {
        alert("抱歉，您的浏览器不支持WebSocket协议!");
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        var chatContent = {};
        chatContent.topic = "websocket1";
        chatContent.contentType = "onUnSubscribe";
        chatContent.content = message;
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(JSON.stringify(chatContent));
        }
        else {
            alert("WebSocket连接没有建立成功!");
        }
    }
    function login(message) {
        if (!window.WebSocket) {
            return;
        }
        var chatContent = {};
        chatContent.topic = "websocket1";
        chatContent.contentType = "login";
        var userInfo = {};
        userInfo.userId = userId;
        userInfo.password = password;
        chatContent.content = JSON.stringify(userInfo);
        chatContent.price = message;
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(JSON.stringify(chatContent));
        }
        else {
            alert("WebSocket连接没有建立成功!");
        }
    }
    function trade(message) {
        if (!window.WebSocket) {
            return;
        }
        var chatContent = {};
        chatContent.topic = "websocket1";
        chatContent.contentType = "tradeBuy";
        chatContent.token = token;
        chatContent.content = message;
        chatContent.price = message;
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(JSON.stringify(chatContent));
        }
        else {
            alert("WebSocket连接没有建立成功!");
        }
    }
</script>
<form onsubmit="return false;">
    <input type="text" name="message" value="我是买家"/>
    <br><br>
    <input type="button" value="登陆" onclick="login(this.form.message.value)"/>
    <hr color="blue"/>
    <h3>服务端返回的应答消息</h3>
    <textarea id="responseText" style="width:500px;height:300px;"></textarea>
</form>
<form onsubmit="return false;">
 	<h3>买家交易</h3>
    <input type="text" name="message" value="12.1"/>
    <br><br>
    <input type="button" value="发送WebSocket请求消息" onclick="trade(this.form.message.value)"/>
    <hr color="blue"/>
    <h3>服务端返回的应答消息</h3>
    <textarea id="responseText1" style="width:500px;height:300px;"></textarea>
</form>
</body>
</html>